package com.sinosoft.hydra.um.services.component.dataimport.validators;

import com.sinosoft.hydra.um.services.component.dataimport.resolvers.ValidateResult;

import java.util.regex.Pattern;

/**
 * Created by Administrator on 2015/1/10.
 */
public class SpecialCharactersValidator extends Validator{


    private String errorMsg;

    public SpecialCharactersValidator() {
    }

    public SpecialCharactersValidator(String errorMsg) {
        this.errorMsg = errorMsg;
    }

    public ValidateResult validate(Object obj) {
        if(obj == null){
            return ValidateResult.PASS();
        }
        try {
            String ss = (String)obj;
            if(Pattern.matches("[~\\!\\@\\#\\$\\%\\^\\&\\*\\(\\)]+", ss)){
                if(errorMsg != null){
                    return ValidateResult.ERROR(errorMsg);
                }
                return ValidateResult.ERROR("数据不能包含特殊字符");
            }
        }catch (Exception ex){
            throw new ValidateException();
        }

        return ValidateResult.PASS();
    }
}
